<template>
  <div class="login-account">
    <el-form
      label-width="70px"
      :model="account"
      :rules="loginAccountRules"
      ref="elFormRef"
      class="el-form"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { ElForm } from 'element-plus'
import { defineComponent, reactive, ref } from 'vue'
import { loginAccountRules } from '../config/account-config'
import localCache from '@/utils/cache'
import { useStore } from 'vuex'

export default defineComponent({
  name: 'LoginAccount',
  setup() {
    const store = useStore()
    const account = reactive({
      name: localCache.getCache('name') ?? '',
      password: localCache.getCache('password') ?? ''
    })
    const elFormRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      elFormRef.value?.validate((valid) => {
        //check通过
        if (valid) {
          //是否需要记住密码
          if (isKeepPassword) {
            // 记住密码
            localCache.setCache('name', account.name)
            localCache.setCache('password', account.password)
            // 开始登陆操作
          }
          //else {
          //   localCache.deleteCache('name')
          //   localCache.deleteCache('password')
          // }
          store.dispatch('login/accountLoginAction', { ...account })
        }
      })
    }
    return { account, loginAccountRules, loginAction, elFormRef }
  }
})
</script>

<style lang="less" scoped>
.el-form {
  padding-top: 10px;
}
</style>
